package com.ztgf.sms.common;

import com.ztgf.common.utils.DateUtils;
import com.ztgf.common.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 *
 * 成都创信短信配置
 */
@Component
public class GetDCPostMsg {
    @Value(value = "${sms.dc.posturl}")
    private String postUrl; //成都创信发送短信地址

    @Value(value = "${sms.dc.balanceurl}")
    private String balanceUrl; //成都创信发送短信地址

    @Value(value = "${sms.dc.userid}")
    private String userid; //用户ID

    @Value(value = "${sms.dc.account}")
    private String account; //账号

    @Value(value = "${sms.dc.password}")
    private String password; //密码

    public String sendSMS(String mobile, String content) {
        try {
            //发送POST请求
            String postData = getPostData(mobile, content);
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
            out.write(postData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
//                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

    public String sendSMS(String mobile, String content,String companyName) {
        try {
            //发送POST请求
           String companyName1="【"+companyName+"】";
            String postData = getPostData(mobile, content,companyName1);
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
            out.write(postData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
//                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

    public String queryBalance() {
        try {
            //发送POST请求
            String balanceData = getBalanceData();
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + balanceData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
            out.write(balanceData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
//                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

    private String getPostData(String mobile, String content){
        StringBuffer postData = new StringBuffer("action=send&rt=json");
        postData.append("&userid=" + userid);
        postData.append("&account=" + account);
        postData.append("&password=" + password);
        postData.append("&mobile=" + mobile);
        postData.append("&content=【中铁工服】" + content);
        postData.append("&sendTime=&extno=");

        return  postData.toString();
    }
    private String getPostData(String mobile, String content,String companyName){
        StringBuffer postData = new StringBuffer("action=send&rt=json");
        postData.append("&userid=" + userid);
        postData.append("&account=" + account);
        postData.append("&password=" + password);
        postData.append("&mobile=" + mobile);
        postData.append("&content="+content+companyName);
        postData.append("&sendTime=&extno=");

        return  postData.toString();
    }

    private String getBalanceData(){
        StringBuffer balanceData = new StringBuffer("action=overage&rt=json");
        balanceData.append("&userid=" + userid);
        balanceData.append("&account=" + account);
        balanceData.append("&password=" + password);
        return  balanceData.toString();
    }

    public String sendOtherSMS(String mobile, String content) {
        try {
            //发送POST请求
            String postData = getOtherPostData(mobile, content);
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
            out.write(postData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
//                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }

    private String getOtherPostData(String mobile, String content){
        StringBuffer postData = new StringBuffer("action=send&rt=json");
        postData.append("&userid=" + userid);
        postData.append("&account=" + account);
        postData.append("&password=" + password);
        postData.append("&mobile=" + mobile);
        postData.append("&content=" + content);
        postData.append("&sendTime=&extno=");

        return  postData.toString();
    }
}
